package com.iflytek.ci.qams.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.iflytek.ci.cums.service.impl.ShiroDbRealm;
import com.iflytek.ci.qams.dao.ActionBatchDao;
import com.iflytek.ci.qams.dao.ActionDao;
import com.iflytek.ci.qams.domain.Action;
import com.iflytek.ci.qams.service.ActionService;

@Service("actionService")
@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
public class ActionServiceImpl implements ActionService {
	@Autowired
	private ActionDao dao;
@Autowired
private ActionBatchDao actionBatchDao;
	@Override
	public Action getById(long id) {
		return dao.getById(id);
	}

	@Override
	public List<Action> getByIssue_Id(long issue_id) {
		return dao.getByIssue_Id(issue_id);
	}

	@Transactional
	@Override
	public void add_issue_action(Action issue_action) {
		dao.add_issue_action(issue_action);
	}

	@Transactional
	@Override
	public void delete_issue_action(Map<String, Object> params) {
		dao.delete_issue_action(params);
	}

	@Transactional
	@Override
	public void add_action(long issue_id, String action_type, String old_value,
			String new_value) {
		ShiroDbRealm.ShiroUser shiro = (ShiroDbRealm.ShiroUser) SecurityUtils
				.getSubject().getPrincipal();
		Action action = new Action();
		action.setAction_time(new Date());
		action.setAction_type(action_type);
		action.setAction_user(shiro.getId());
		action.setAction_username(shiro.getName());
		action.setIssue(issue_id);
		action.setNew_value(new_value);
		action.setOld_value(old_value);
		dao.add_issue_action(action);
	}
	
	@Transactional
	@Override
	public void add_batch_action(String[] issue_id, String action_type, List<String> old_value,
			String new_value){
		ShiroDbRealm.ShiroUser shiro = (ShiroDbRealm.ShiroUser) SecurityUtils
		.getSubject().getPrincipal();
		List<Action>actions=new ArrayList<Action>();
		for(int i=0;i<issue_id.length;i++){
			Action action = new Action();
			action.setAction_time(new Date());
			action.setAction_type(action_type);
			action.setAction_user(shiro.getId());
			action.setAction_username(shiro.getName());
			action.setIssue(Long.valueOf(issue_id[i]));
			action.setNew_value(new_value);
			action.setOld_value(old_value.get(i));
			actions.add(action);
		}
		actionBatchDao.batch_action(actions);
	}
}